#!/usr/bin/env node
/**
 * 活动状态更新脚本
 * 用于手动或定时更新活动状态
 * 可以作为cron任务运行
 */

const { promisePool } = require('../config/database');

async function updateActivityStatus() {
  try {
    
    // 调用存储过程更新所有活动状态
    const [result] = await promisePool.execute('CALL sp_batch_update_all_activities()');
    
    
    // 获取最近的状态变更日志
    const [logs] = await promisePool.execute(`
      SELECT 
        asl.*,
        a.title as activity_title
      FROM activity_status_log asl
      LEFT JOIN activities a ON asl.activity_id = a.id
      ORDER BY asl.created_at DESC
      LIMIT 10
    `);
    
    
    return {
      success: true,
      message: '活动状态更新完成',
      logs: logs
    };
  } catch (error) {
    return {
      success: false,
      message: error.message
    };
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  updateActivityStatus()
    .then(result => {
      process.exit(result.success ? 0 : 1);
    })
    .catch(error => {
      process.exit(1);
    });
}

module.exports = {
  updateActivityStatus
};
